import StringIO
import os
import os.path
import struct

import Image
import ImagePalette

import Utils.Utils

class BSALightMap:
    '''There are two lightmap files: "FOG.LGT" and "NORMAL.LGT".  Each of these
    files is 13 * 256 bytes long, arranged as [13][256] where row[0][x] is
    the normal color map.  The color values fad towards black (or fog) so
    that [12][x] is the darkest version of the color.

    However, this is reversed from what I'm used to (bigger numbers indicating
    brighter lights), so loading the light maps will rearranged with [0][x]
    being the darkest version of each color, [12][x] will be the brightest.

    To further help the renderer, extra padding is added before and after
    these lines.  This helps with some look-up indexing, and also is useful
    for the dithering effect Arena does on pixel brightness to minimize the
    obvious color banding along light gradients.
    '''
    
    RAMPS_X = 256
    RAMPS_Y = 13
    LIGHTMAP_X = 18
    LIGHTMAP_Y = 256
    
    def __init__(self, filename = '', palette_red_first = None):
        self._filename = filename
        self._ramps = [ ['' for y in xrange(self.RAMPS_X)] for x in xrange(self.RAMPS_Y) ]
        self._flattened_ramps = ['' for i in xrange(self.RAMPS_Y * self.RAMPS_X)]
        self._lightmap = ['' for x in xrange(self.LIGHTMAP_X * self.LIGHTMAP_Y)] 
        #self._palette_red_first = palette_red_first
        self._palette_red_first = palette_red_first.flatten()
        
    def __repr__(self):
        return 'BSALightMap: [filename=' + self._filename + '|ramps=' + str(self._ramps) +\
            '|lightmap=' + str(self._lightmap) + '|flattened_ramps=' + str(self._flattened_ramps) + ']'
    
    # getters
    def get_filename(self):
        return self._filename
    
    def get_ramps(self):
        return self._ramps
    
    def get_lightmap(self):
        return self._lightmap
    
    def get_buffer(self):
        return self._lightmap
    
    def get_name(self):
        base_name = os.path.basename(self._filename)
        return os.path.splitext(base_name)[0]

    def get_flattened_ramps(self):
        return self._flattened_ramps

    # setters
    def set_filename(self, filename):
        self._filename = filename
        
    def set_lightmap(self, lightmap):
        self._lightmap = lightmap
    
    def set_buffer(self, buffer):
        self._lightmap = buffer
         
    def load(self):
        f = open(self._filename, 'rb')
                
        r = f.read()

        f.close()
        
        fmt = str(self.RAMPS_Y * self.RAMPS_X) + 'B'
        self._ramps = struct.unpack(fmt, r)
        
        for bright in xrange(self.RAMPS_Y):
            for index in xrange(self.RAMPS_X):
                self._lightmap[((bright + 1) << 8) + index] = self._ramps[12 - bright + index]
        
        for index in xrange(self.RAMPS_X):
            self._lightmap[( 0 << 8) + index] = self._ramps[12 + index]
            self._lightmap[(13 << 8) + index] = self._ramps[0  + index]
            self._lightmap[(14 << 8) + index] = self._ramps[0  + index]
            self._lightmap[(15 << 8) + index] = self._ramps[0  + index]
            
    def dump(self, filename):
        gif_img = Image.new('P', (self.RAMPS_X, self.RAMPS_Y))
        
        s = StringIO.StringIO(self._ramps)

        gif_img.putpalette(self._palette_red_first)
        gif_img.format = 'GIF'

        gif_img.putdata(self._ramps)
                   
        print gif_img.format, gif_img.size, gif_img.mode
        gif_img.save(filename)